<?php
class Photos extends common{ 
    
    public function __construct(){
        $this->photos();
    }
    
    public function photos(){
        $this->assgin('cons_type', array(
                                     '1'=>'新娘发型',
                                     '2'=>'长发',
                                     '3'=>'短发',
                                     '4'=>'波波头',
                                     '5'=>'卷发',
                                     '6'=>'晚会发型',
                                     '7'=>'约会发型'
                                   ));
    }
    
    public function index(){
/*    	$mod=&D('Special');
        $kk=$mod->r_select(array(
          'field'=>'id,sp_name,sp_logo,is_poster',
          'where'=>array('is_del'=>0),
          'join'=>array(
             array('PhotoSpecial','photo_id','special_id','id')
             )
           ));exit('end28');*/
    	import('page');
        $pmod=&D('Photos');
        $cateIds=$pmod->r_select(array(
                 'field'=>'id,photo_name,add_time',
                 'where'=>array('is_del'=>0),
                 'join'=>array(
                          array('PhotoCate','','photo_id',array('pc','','',array())),
                          array('PhotoSpecial','','photo_id',array('ps','','',array()))
                        ),
                  'limit'=>array(page::$curr,page::$record),
               ));
          echo $pmod->getPageCount();//exit('f');
         //开始分类操作，提取分类id
        $cateId=array();
        $spId=array();
        foreach ($cateIds as $cv){
            $cateId[$cv['id']]='';
            $spId[$cv['id']]='';
            foreach ($cv['pc'] as $pv)
              $cateId[$cv['id']].=$pv['cate_id'].',';
            foreach ($cv['ps'] as $spv)
              !empty($spv) && $spId[$cv['id']].=$spv['special_id'].',';
        }
        $cate_mod=&D('Category');
        $cate=array();
        foreach ($cateId as $cik=>$civ){
          $cates=$cate_mod->select(array(
            'where'=>array('is_del'=>0,'in'=>array('id'=>rtrim($civ,',')))
          ));
          
          foreach ($cates as $csv){
              if ($csv['pid']==0)
                $cate[$cik]['parent'][]=$csv['cate_name'];
              else 
                $cate[$cik]['child'][]=$csv['cate_name'];
          }
        }
        //专题
        $pmod=&D('Special');
        $pss=array();
        foreach ($spId as $spk=>$spv){
//          if (!empty($spv)){
        	$tmp=$pmod->select(array(
        	         'field'=>'sp_name',
        	         'where'=>array('is_del'=>0,'in'=>array('id'=>rtrim($spv,',')))
        	     ));
        	$pss[$spk]=$tmp;
//          }
        }
        
        
        $new=array();
        foreach ($cateIds as $cisv){
        	$tmp=array();
            if (isset($cate[$cisv['id']])){
                $tmp=array('id'=>$cisv['id'],'photo_name'=>$cisv['photo_name'],'add_time'=>$cisv['add_time'],'cate'=>array('parent'=>$cate[$cisv['id']]['parent'],'child'=>$cate[$cisv['id']]['child']) );
            }
            if (isset($pss[$cisv['id']])){
            	$tmp['special']=$pss[$cisv['id']];
            }
            $new[]=$tmp;
        }
        
        //var_dump($new);exit;
        //开始专题
/*        $sp_mod=&D('PhotoSpecial');
        $sp=$sp_mod->r_select(array(
            'join'=>array(),
        ));*/
        
        $this->assgin('photo', $new);
        $this->display();
    }
    
    
    
    public function add(){
        $faceType=array('长型脸','鹅蛋脸','方形脸','瓜子脸','菱形脸','梨形脸','圆形脸');
        $mod=&D('Category');
        $cate=$mod->select(array(
            'where'=>array('is_del'=>0),
            'orderby'=>'pid asc',
        ));
        $cates=array();
        foreach ($cate as $cKey=>$cVal){
            if ($cVal['pid']==0)
              $cates[$cVal['id']]=array('title'=>$cVal['cate_name']);
            else 
              $cates[$cVal['pid']]['sub'][$cVal['id']]=$cVal['cate_name'];
        }
/*        $sp_mod=&D('Special');
        $sp=$sp_mod->select(array(
             'field'=>'id,sp_name',
             'where'=>array('is_del'=>0),
        ));*/
        $this->assgin('ft', $faceType);
//        $this->assgin('sp', $sp);
        $this->assgin('cates', $cates);
        $this->display();
    }
    
    public function doAdd(){
        $data=array();
        $data['photo_name']=isset($_POST['name'])?$_POST['name']:'';
        $data['photo_small']=isset($_POST['imgLogo'])?$_POST['imgLogo']:'';
        $data['recommend']=isset($_POST['recommend'])?$_POST['recommend']:0;
        $data['intro']=isset($_POST['intro'])?$_POST['intro']:'';
        $data['xcoord']=isset($_POST['xcoord'])?$_POST['xcoord']:0;
        $data['ycoord']=isset($_POST['ycoord'])?$_POST['ycoord']:0;
        $data['add_time']=time();
        $mod=&D('Photos');
        $photoId=$mod->add($data);
        $cate_mod=&D('PhotoCate');
        if ((isset($_POST['cate']) && empty($_POST['cate'])) || !isset($_POST['cate']))$_POST['cate']=array();
        foreach ($_POST['cate'] as $pcv){
            $cate_mod->add(array('cate_id'=>$pcv,'photo_id'=>$photoId));
            
        }
        $img_mod=&D('Imgs');
        $pi_mod=&D('PhotoImg');
        $imgSet=explode(',', $_POST['imgSet']);
        foreach ($imgSet as $isv){
            if (!empty($isv)){
              $imgId=$img_mod->add(array('path'=>$isv,'add_time'=>time()));
              $pi_mod->add(array('photo_id'=>$photoId,'img_id'=>$imgId));
            }
        }
        $ft_mod=&D('PhotoFace');
        if ((isset($_POST['faceType']) && empty($_POST['faceType'])) || !isset($_POST['faceType']))$_POST['faceType']=array();
        foreach ($_POST['faceType'] as $ftv){
            //1长型脸 2 鹅蛋脸 3 方形脸 4 瓜子脸 5 菱形脸 6 梨形脸 7 圆形脸
            $ft_mod->add(array('photo_id'=>$photoId,'face_id'=>$ftv));
        }
        header('refresh:1,url="?m=photos&a=add"');
        exit('添加成功,一秒钟后自动跳转。');
    }
    
    
    public function upload(){
        import('Upload');
        if ($_GET['s']=='set'){
          $upload=new Upload('photoSet');
        }elseif ($_GET['s']=='logo'){
          $upload=new Upload('photoLogo');
        }
        $upload->path('set');
        $upload->doUpload();
        echo '{img:"'.$upload->getFile().'"}';
    }
    
    
    
    
    //分类管理
    
    public function category(){
        $mod=&D('category');
        $cates=$mod->select(array(
             'where'=>array('is_del'=>0),
             'orderby'=>'pid asc'
        ));
        $sub=array();
        foreach ($cates as $cK=>$cV){
            if ($cV['pid']==0){
               $sub[$cV['id']]['cate']=array($cV['cate_name']);
               $sub[$cV['id']]['time']=$cV['add_time'];
            }else{
               @$sub[$cV['pid']]['cate'][1].=$cV['cate_name'].',';
               $sub[$cV['pid']]['time']=$cV['add_time'];
            }
        }
        $this->assgin('cates', $sub);
        $this->display();
    }
    
    
    public function cate_add(){
        $mod=&D('category');
        $cates=$mod->select(array(
             'field'=>'id,cate_name',
             'where'=>array('is_del'=>0),
        ));
        $this->assgin('cates', $cates);
        $this->assgin('backUrl', '?m=photos&a=category');
        $this->display();
    }
    
    public function cate_update(){//$a='1,2,3,4';var_dump(is_numeric($a[0]));exit;
        $special=isset($_GET['special'])?$_GET['special']:false;
        $id=isset($_GET['id']) && is_numeric($_GET['id'])?$_GET['id']:-1;
        $cates=array();
            $mod=&D('category');
            
            //$mod->r_select(array('join'=>array('PhotosSpecial','name,id','photo_id')));exit;
            
            if ($special=='modify'){
                $data=array();
                isset($_POST['cate_name']) && !empty($_POST['cate_name']) && $id=intval($_POST['cate_name']);
                isset($_POST['cate_sub']) && !empty($_POST['cate_sub']) && $data['cate_name']=$_POST['cate_sub'];
                isset($_POST['img']) && !empty($_POST['img']) && $data['img']=serialize($_POST['img']);
                $affect=$mod->update(array(
                    'where'=>array('is_del'=>0,'id'=>$id),
                    'data'=>$data,
                ));
                header('refresh:1,url="?m=photos&a=category"');
                if ($affect){
                  exit('更新成功,一秒钟后自动跳转');
                }else{
                  echo('更新失败,一秒钟后自动跳转');
                }
            }
        if ($id!==-1){
/*            $mod->update(array(
                 'where'=>array('is_del'=>0,'id'=>$id)
            ));*/
            $cates=$mod->select(array(
                'where'=>array('is_del'=>0,array('id'=>$id,'_logic'=>'or','pid'=>$id)),
                'orderby'=>'pid asc'
            ));
        }
        $img=unserialize($cates[0]['img']);
        $this->assgin('img', $img);
        $this->assgin('id', $id);
        $this->assgin('backUrl', '?m=photos&a=category');
        $this->assgin('cates', $cates);
        $this->display();
//        header('refresh:3;url="?m=photos&a=category"');
//        exit('更新失败，3秒钟后自动跳转。');
    }
    
    
    public function cate_confirm(){
        $mod=&D('category');
        $data['pid']=intval($_POST['cate_name']);
        $data['cate_name']=$_POST['cate_sub'];
        $data['img']=!$data['pid']?serialize($_POST['img']):'';
        $data['add_time']=time();
        $mod->add($data);
        
        header('location:index.php?m=photos&a=cate_add');
    }
    
    public function cate_upload(){
        import('upload');
        $upload=new Upload('catefile');
        $upload->path('category');
        $upload->doUpload();
        echo '{img:"'.$upload->getFile().'"}';
    }
    
    
}